<!-- 我的弹窗 -->

<template>
  <WebModal word="人脸库" pass="布控" rej="撤控" small>
    <WebForm word6>
      <FormInput label="人脸库名称" bind="name" :max="6" required />

      <FormTextarea label="备注" bind="remark" :max="20" required />

      <FormSlot label="关联人脸设备">
        <span v-for="item in one.faceDeviceDto" :key="item.id" class="el-button--text" @click="toOpen(item.id)">
          <span>{{ item.deviceName }}</span>
        </span>

        <span v-if="!isShow">
          <BtnText v-if="one.faceDeviceDto.length" title="清除" @click="toClear" />
          <BtnPrimary v-else title="选择" @click="toOpen" />
        </span>
      </FormSlot>
    </WebForm>

    <EasyModal v-model="easyVisible" title="选择设备" large @submit="toSubmit" @close="toReset">
      <ChooseIOT :obj="obj" bind="deviceList" deviceCategoryId="1" disableCategory />
    </EasyModal>
  </WebModal>
</template>

<script>
// ============================== 导入依赖 ============================== //

import CORE from '@/CORE'

// ============================== 导出组件 ============================== //

export default {
  /**
   * 名称定义 (实际使用名称)
   */
  name: 'MyModal',

  /**
   * 混入列表 (配置选项合并)
   */
  mixins: [CORE.mixWeb()],

  /**
   * 组件导入 (Abc.vue + Abc/index.vue)
   */
  ...CORE.components(require.context('./', true, /vue$/)),

  /**
   * 状态数据 (函数返回对象)
   */
  data() {
    return {
      easyVisible: false, // 弹窗是否可见
      obj: { deviceList: [] }, // 数据对象
    }
  },

  /**
   * 监听属性 (深度 deep 首次 immediate)
   */
  watch: {
    /**
     * 监听弹窗可见
     */
    easyVisible(visible) {
      visible && this.toReset()
    },
  },

  /**
   * 本地方法 (轻量重要在前)
   */
  methods: {
    /**
     * 弹窗
     */
    toOpen() {
      this.easyVisible = true
    },

    /**
     * 提交
     */
    toSubmit() {
      const { obj } = this
      this.easyVisible = false
      this.one.faceDeviceDto = obj.deviceList.map((item) => ({
        deviceId: item.id,
        deviceName: item.deviceName,
        deviceCategoryId: item.deviceCategoryId,
        deviceTypeId: item.deviceTypeId,
      }))
      this.one.deviceId = obj.deviceList.map(({ id }) => id)
    },

    /**
     * 清除
     */
    toClear() {
      this.one.faceDeviceDto = []
      this.one.deviceId = []
      this.obj = { deviceList: [] }
    },

    /**
     * 重置
     */
    toReset() {
      const deviceList = this.one.faceDeviceDto.map((item) => ({
        id: item.deviceId,
        deviceName: item.deviceName,
        deviceCategoryId: item.deviceCategoryId,
        deviceTypeId: item.deviceTypeId,
      }))
      this.obj = { deviceList }
    },
  },
}
</script>
